/*
	Minable Asteroid Ent for McBuilds' Mining
	
	Author: Steeveeo
*/

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include("shared.lua")

//---
//--All of this will be set by the server, these are just placeholders!--
//---

//Default hold values
ENT.Ore = 4500 //Ore gets processed into other resources, not defined here
ENT.Rubble = 5000 //Useless rock, this just gets in the way
ENT.Ice = 100
ENT.Total = ENT.Ore + ENT.Rubble + ENT.Ice //When this is 0, poof

//This is the type of asteroid, all this is for is picking the model
ENT.RoidType = 1

//--End of Placeholders--

//This is called right before this spawns,
//Server should already have provided values by now.
function ENT:Initialize()
	self.Entity:SetName( "Asteroid" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS ) 	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS ) 

	self.SC_Immune = true --make weapons ignore me
	self.SB_Ignore = true --make spacebuild ignore me
	self.Untouchable = true
	self.Unconstrainable = true
	self.PhysgunDisabled = true
	self.CanTool = function( ply, trace, mode )
		return false
	end
	
	//This line is for SPP to not let people "Own" the roids
	self:SetNetworkedString("Owner","World")

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Sleep()
		phys:EnableGravity( false )
		phys:EnableCollisions( true )
		phys:EnableMotion( false )
		phys:SetMass( 50000 )
	end
end

function ENT:Think()
	
	//Check if we're out of contents
	self.Total = self.Ore + self.Rubble + self.Ice //When this is 0, poof
	if self.Total == 0 then
		self:Remove()
	end
	
	//This is just a debug thing, if ever removed like this it means I fudged the math -Steeveeo
	if self.Total < 0 then
		self:Remove()
		Error("MINING: Steeveeo! You fucked up the math!")
	end
	
	self.Entity:NextThink( CurTime() + 1 )
	return true
end

//Mining Function, returns table of mined elements
function ENT:MiningSequence(resourceCount)
	local Receive = {}
	
	//Generate a random number for each resource, the rest is just rubble
	math.randomseed(os.time())
	local OreAmt = math.Rand(0,self.Ore/self.Total)
	local IceAmt = math.Rand(0,self.Ice/self.Total)
	
	//Make the actual values
	local resourceBuffer = 50 //Value added in order to not have extremely low returns while contents remain
	Receive["Ore"] = math.Clamp( math.floor(OreAmt * resourceCount) + resourceBuffer, 0, self.Ore )
	Receive["Ice"] = math.Clamp( math.floor(IceAmt * resourceCount) + resourceBuffer, 0, self.Ice )
	Receive["Rubble"] = math.Clamp( math.floor(resourceCount - (Receive["Ore"] + Receive["Ice"])), 0, self.Rubble )
	//We will never use this in resourcing, but it's just so we can return it on the mining beam.
	
	//Subtract from our hold
	self.Ore = self.Ore - Receive["Ore"]
	self.Rubble = self.Rubble - Receive["Rubble"]
	self.Ice = self.Ice - Receive["Ice"]
	
	return Receive
end

//Placeholder functions
function ENT:Touch()
end

function ENT:StartTouch()
end

function ENT:EndTouch()
end

